﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Cart.Data;
using TWS.Cart.Business;

public partial class Application_Controls_Products : System.Web.UI.UserControl
{
	string permanentPath;

	protected void Page_Load(object sender, EventArgs e)
	{
		permanentPath = ConfigurationManager.AppSettings["FilePath"].ToString();

		string catId = Request.QueryString["catId"];
		if (string.IsNullOrEmpty(catId))
			catId = selectDefaultCategoryId();

		CartCatCategory ssCat = CartCatCategory.FetchByID(catId);

		if (ssCat != null)
			LiteralForCategoryTitle.Text = ssCat.Description;			

		//Load Data.
		ssColProducts = new ViewCartProductCollection()
		.Where(CartCatProduct.Columns.CategoriesId, catId)
		.Load();                

		DrawProducts();
    }

	private string selectDefaultCategoryId()
	{
		string res = null;
		var ssCategory = new CartCatCategoryCollection()
			.Where(CartCatCategory.Columns.CategoryOrder,SubSonic.Comparison.Equals,10)
			.Load().SingleOrDefault();

		if (ssCategory != null)
			res = ssCategory.Id.ToString();

		return res;
	}

    private ViewCartProductCollection _ssColProducts;

	public ViewCartProductCollection ssColProducts
    {
        get { return _ssColProducts; }
        set { _ssColProducts = value; }
    }

	protected void DrawProducts()
	{
		foreach (ViewCartProduct ssProduct in ssColProducts)
		{
			TableRow tr = new TableRow();
			TableRow tr1 = new TableRow();
			TableRow tr2 = new TableRow();
			TableRow trDivider = new TableRow();

			TableCell tc00 = new TableCell();
			TableCell tc01 = new TableCell();
			TableCell tc02 = new TableCell();

			TableCell tc10 = new TableCell();
			TableCell tc11 = new TableCell();

			TableCell tc20 = new TableCell();
			TableCell tcDivider = new TableCell();

			//The image
			Image img = new Image();
			img.ImageUrl = permanentPath + ssProduct.FileName;
			img.BorderWidth = 2;
			img.CssClass = "ImageBorder";
			tc00.HorizontalAlign = HorizontalAlign.Center;
			tc00.VerticalAlign = VerticalAlign.Middle;
			tc00.Controls.Add(img);

			tc01.Controls.Add(GetProdDetails(ssProduct));

			tc02.Controls.Add(GetOtherDetails(ssProduct));

			//Load CartConfigVals
			CartCatConfigurationParameter ssConfigParam = new CartCatConfigurationParameter(4);

			HyperLink hl = new HyperLink();
			hl.Text = Resources.Resource.SeeMoreDetail;
			hl.NavigateUrl = ssConfigParam .ValueX + "?prodId=" + ssProduct.Id;
			tc10.Controls.Add(hl);
			tc10.ColumnSpan = 2;
			tc10.HorizontalAlign = HorizontalAlign.Center;

			tc11.Text = ssProduct.DescriptionShort;
			tc11.ColumnSpan = 2;

			tcDivider.ColumnSpan = 3;
			tcDivider.Text = "<hr>";
			trDivider.Cells.Add(tcDivider);

			tr.Cells.Add(tc00);
			tr.Cells.Add(tc01);
			tr.Cells.Add(tc02);

			tr1.Cells.Add(tc11);

			tr2.Cells.Add(tc10);

			TableProducts.Rows.Add(tr);
			TableProducts.Rows.Add(tr1);
			TableProducts.Rows.Add(tr2);
			TableProducts.Rows.Add(trDivider);
		}
	}

	protected Table GetProdDetails(ViewCartProduct ssProduct)
	{
		//The table infront for product details.
		Table tableProductDetail = new Table();
		//tableProductDetail.GridLines = GridLines.Both;

		//Product
		TableRow trDetail0 = new TableRow();
		TableCell tcDet00 = new TableCell();
		TableCell tcDet01 = new TableCell();
		tcDet00.Text = Resources.Resource.Product;
		tcDet01.Text = ssProduct.Name;
		trDetail0.Cells.Add(tcDet00);
		trDetail0.Cells.Add(tcDet01);
		tableProductDetail.Rows.Add(trDetail0);

		//Model or SKU
		TableRow trDetail1 = new TableRow();
		TableCell tcDet10 = new TableCell();
		TableCell tcDet11 = new TableCell();
		tcDet10.Text = Resources.Resource.Model;
		tcDet11.Text = ssProduct.Sku;
		trDetail1.Cells.Add(tcDet10);
		trDetail1.Cells.Add(tcDet11);
		tableProductDetail.Rows.Add(trDetail1);

		//Price
		TableRow trDetail2 = new TableRow();
		TableCell tcDet20 = new TableCell();
		TableCell tcDet21 = new TableCell();
		tcDet20.Text = Resources.Resource.Price;
		tcDet21.Text = ssProduct.Price.ToString("c");
		trDetail2.Cells.Add(tcDet20);
		trDetail2.Cells.Add(tcDet21);
		tableProductDetail.Rows.Add(trDetail2);

		//Size
		TableRow trDetail3 = new TableRow();
		TableCell tcDet30 = new TableCell();
		TableCell tcDet31 = new TableCell();
		tcDet30.Text = Resources.Resource.Size;
		tcDet31.Text = Resources.Resource.Width+":"+ssProduct.SizeWidth+"<br>";
		tcDet31.Text += Resources.Resource.Height + ":" + ssProduct.SizeHeight;
		trDetail3.Cells.Add(tcDet30);
		trDetail3.Cells.Add(tcDet31);
		tableProductDetail.Rows.Add(trDetail3);

		return tableProductDetail;
	}

	protected Table GetOtherDetails(ViewCartProduct ssProduct)
	{
		Table res =new Table();
		//res.GridLines = GridLines.Both;
		res.Width = Unit.Percentage(100);

		TableRow tr0 = new TableRow();
		TableRow tr1 = new TableRow();

		TableCell tc00=new TableCell();
		TableCell tc01 = new TableCell();

		TableCell tc10 = new TableCell();
		TableCell tc11 = new TableCell();

		//The Quantity
		tc00.Text = Resources.Resource.Quantity+":";
		TextBox tb = new TextBox();
		tb.Width = Unit.Pixel(20);
		tb.Text = "1";
		tb.ID = "TextBoxQuantity" + ssProduct.Id;
		tc01.Controls.Add(tb);

		//Check if more attibutes are available and add them to the product
		CartCatAttributeCollection ccaColl = new CartCatAttributeCollection()
				.Where(CartCatAttribute.Columns.ProductsId,ssProduct.Id)
				.Load();

			if (ccaColl.Count > 0)
			{
				foreach (var prodAttribute in ccaColl)
				{
					Control c = null;

					switch (prodAttribute.AttributeTypeId)
					{
						case 1:
							TextBox tbb = new TextBox();
							tbb.ToolTip = prodAttribute.Description;
							c = tbb;
							break;
						case 2:
							CartTranAttributeValueCollection ctavColl = new CartTranAttributeValueCollection()
								.Where(CartTranAttributeValue.Columns.AttributesId,prodAttribute.Id)
								.Load();
							DropDownList ddl = new DropDownList();
							ddl.Items.Add(new ListItem(Resources.Resource.SelectAValue,""));
							foreach (var ctavItem in ctavColl)
							{
								ddl.Items.Add(new ListItem(ctavItem.ValueX,ctavItem.Id.ToString()));	
							}
							ddl.ToolTip = prodAttribute.Description;
							c = ddl;
							break;
						case 3:
							TextBox ta = new TextBox();
							ta.TextMode = TextBoxMode.MultiLine;
							ta.ToolTip = prodAttribute.Description;
							c = ta;
							break;
						case 0:
							Literal l = new Literal();
							l.Text = "Not defined";
							c = l;
							break;
					}

					c.ID = "prodAttrib"+prodAttribute.Id;

					TableRow tr = new TableRow();
					TableCell tc0 = new TableCell();
					TableCell tc1 = new TableCell();

					tc0.Text = prodAttribute.Name + ":";
					tc1.Controls.Add(c);

					tr.Cells.Add(tc0);
					tr.Cells.Add(tc1);

					res.Rows.Add(tr);
				}
			}

		//The buttons
		Button b1 = new Button();
		b1.Width = Unit.Pixel(80);
		b1.ID = "ButtonAddToCart" + ssProduct.Id;
		b1.Click += new EventHandler(b1_Click);
		b1.Text = Resources.Resource.AddToCart;
		
		Button b2 = new Button();
		b2.Width = Unit.Pixel(80);
		b2.Click += new EventHandler(ButtonCheckout_Click);
		b2.Text = Resources.Resource.Checkout;

		Literal LiteralDivider = new Literal();
		LiteralDivider.Text = "<br>";

		tc11.HorizontalAlign = HorizontalAlign.Right;
		tc11.Controls.Add(b1);
		tc11.Controls.Add(LiteralDivider);
		tc11.Controls.Add(b2);

		
		tc11.HorizontalAlign = HorizontalAlign.Right;
		tc11.Controls.Add(b2);

		tr0.Cells.Add(tc00);
		tr0.Cells.Add(tc01);

		tr1.Cells.Add(tc10);
		tr1.Cells.Add(tc11);

		res.Rows.Add(tr0);
		res.Rows.Add(tr1);

		return res;
	}

	void ButtonCheckout_Click(object sender, EventArgs e)
	{
		//Load CartConfigVals
		CartCatConfigurationParameter ssConfigParam = new CartCatConfigurationParameter(3);
		Response.Redirect(ssConfigParam.ValueX);
	}

	void b1_Click(object sender, EventArgs e)
	{
		ShoppingCartManager scm = new ShoppingCartManager();

		Button b = (Button) sender;
		string Id = b.ID.Substring(15);
		int prodId = Convert.ToInt32(Id);
		TextBox tb = (TextBox)FindControl("TextBoxQuantity" + prodId);
		CartCatProduct ssProduct = new CartCatProduct(prodId);

		scm.LstItems.Add(new CartItem(Convert.ToInt32(ssProduct.Id), "x.jpg", ssProduct.Name, ssProduct.Sku, Convert.ToInt32(tb.Text), ssProduct.Price,GetProdAttributes(prodId)));		

		MessageBox01.Visible = true;
		MessageBox01.Text = tb.Text + " " + Resources.Resource.ProductAddedToCart;

		tb.Text = "1";
	}

	protected List<ProdAttribute> GetProdAttributes(int prodId)
	{
		List<ProdAttribute> res = new List<ProdAttribute>();

		//Check if more attibutes are available and add them to the product
		CartCatAttributeCollection ccaColl = new CartCatAttributeCollection()
				.Where(CartCatAttribute.Columns.ProductsId, prodId)
				.Load();

		if (ccaColl.Count > 0)
		{
			foreach (var prodAttribute in ccaColl)
			{
				Control c = FindControl("prodAttrib" + prodAttribute.Id);
				switch (prodAttribute.AttributeTypeId)
				{
					case 1:
						TextBox tbb = (TextBox)c;
						if (!String.IsNullOrEmpty(tbb.Text))
							res.Add(new ProdAttribute(prodAttribute.Id,prodAttribute.Name, tbb.Text));
						tbb.Text = "";
						break;
					case 2:
						DropDownList ddl = (DropDownList)c;
						if (!String.IsNullOrEmpty(ddl.SelectedValue))
							res.Add(new ProdAttribute((long)Convert.ToInt32(ddl.SelectedValue),prodAttribute.Name, ddl.SelectedItem.Text));
						ddl.SelectedValue = "";
						break;
					case 3:
						TextBox ta = (TextBox)c;
						if (!String.IsNullOrEmpty(ta.Text))
							res.Add(new ProdAttribute(prodAttribute.Id,prodAttribute.Name, ta.Text));
						ta.Text = "";
						break;
					case 0:
						res.Add(new ProdAttribute(0,"Not defined", "Not defined"));
						break;
				}
			}
		}

		return res;
	}
}
